package com.np.retrofit211demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.np.retrofit211demo1.service.IAccessToken;
import com.np.retrofit211demo1.service.IUser;
import com.np.retrofit211demo1.util.GsonUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String HOST_BAIDU = "http://www.baidu.com";
    private static final String HOST_SYSTEM = "http://192.168.2.23:7002/system-service/";
    private Retrofit mUserRetrofit;
    private Retrofit mAccessTokenRetrofit;

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getAccessToken();
    }

    private void baidu() {
        mUserRetrofit = new Retrofit.Builder()
                .baseUrl(HOST_BAIDU)
                .build();

        IUser service = mUserRetrofit.create(IUser.class);
        service.getUser(2).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.i(TAG, "response: " + response.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    private void getAccessToken() {

        mAccessTokenRetrofit = new Retrofit.Builder()
                .baseUrl(HOST_SYSTEM)
                .build();

        IAccessToken service = mAccessTokenRetrofit.create(IAccessToken.class);
        service.login(getJSONRequestBodyFromParams(
                "username", "admin",
                "password", "admin@123456",
                "mobile", "13543893247",
                "captcha", "12310"
        )).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.i(TAG, "response: " + response.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    public static RequestBody getJSONRequestBodyFromParams(String... params) {

        if (params.length == 0 || params.length % 2 != 0) {
            throw new IllegalArgumentException("参数的数量不正确");
        }

        HashMap<String, String> map = new HashMap<>();

        for (int i = 0; i * 2 + 1 < params.length; i++) {
            map.put(params[i * 2], params[i * 2 + 1]);
        }

        return getJSONRequestBodyFromEntries(map);


    }

    public static RequestBody getJSONRequestBodyFromEntries(Map<String, String> paramsMap) {

        String content = GsonUtil.gson().toJson(paramsMap);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), content);

        return body;

    }
}